/*
 * ListDialog.java
 *
 * Created on __DATE__, __TIME__
 */

package org.keyroy.map_editor.ui.dialog;

import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * 
 * @author __USER__
 */
public class ListDialog extends javax.swing.JDialog {
	private static final long serialVersionUID = 1L;
	private List<String> strings = new ArrayList<String>();
	private SelectedListener listener;

	public ListDialog(java.awt.Frame parent, List<String> strings, final SelectedListener listener) {
		super(parent, true);
		this.strings = strings;
		this.listener = listener;
		initComponents();
		jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		jList1.addListSelectionListener(new ListSelectionListener() {
			int selectedIndex = -1;

			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (jList1.getSelectedIndex() != selectedIndex) {
					listener.onSelected(jList1.getSelectedIndex());
				}
				selectedIndex = jList1.getSelectedIndex();
			}
		});

	}

	/** Creates new form ListDialog */
	private ListDialog(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		jList1 = new javax.swing.JList();
		jButton1 = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("\u9009\u62e9\u52a0\u8f7d\u7684\u5730\u56fe");

		jList1.setModel(new ListModel());
		jScrollPane1.setViewportView(jList1);

		jButton1.setText("\u9009\u62e9");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				javax.swing.GroupLayout.Alignment.TRAILING,
				layout.createSequentialGroup().addContainerGap().addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jScrollPane1,
								javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 156,
								Short.MAX_VALUE).addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING,
								javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				javax.swing.GroupLayout.Alignment.TRAILING,
				layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1,
						javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE).addPreferredGap(
						javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton1).addContainerGap()));

		pack();
	}// </editor-fold>

	// GEN-END:initComponents

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		dispose();
		listener.onOK(jList1.getSelectedIndex());
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				ListDialog dialog = new ListDialog(new javax.swing.JFrame(), true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}

	private class ListModel extends AbstractListModel {

		private static final long serialVersionUID = 1L;

		@Override
		public Object getElementAt(int index) {
			return strings.get(index);
		}

		@Override
		public int getSize() {
			return strings.size();
		}

	}

	public interface SelectedListener {
		public void onSelected(int index);

		public void onOK(int index);
	}

	// GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JList jList1;
	private javax.swing.JScrollPane jScrollPane1;
	// End of variables declaration//GEN-END:variables

}